package com.wephone.Student.ui;

import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.BaseApplication;
import com.wephone.Student.R;

import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.GroupResponse;
import com.wephone.Student.bean.data.GroupInfo;
import com.wephone.Student.bean.data.MyLocationInfo;
import com.wephone.Student.bean.data.UserInfoData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;
import com.wephone.Student.util.ShareReferenceUtil;
import com.wephone.Student.util.Util;


public class GroupSettingActivity extends BaseActivity {
	private GroupInfo groupData;
	private ProgressDialog progressDialog;
	private RelativeLayout clear_msg,invite_member,tousu;
	private Button btn_quit_group;
	private CheckBox checkbox_msg_recevied,checkbox_msg_tips;
	
	//获取详情
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(GroupSettingActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				if(getDataSuccess){
					setResult(110);
					finish();
					Toast.makeText(getApplicationContext(), "操作成功", Toast.LENGTH_SHORT).show();
				}
			}
			@Override
			public void onFailure(Throwable arg0, String result) {
				super.onFailure(arg0, result);
				
				if(progressDialog!=null){
					progressDialog.dismiss();
				}
				Toast.makeText(getApplicationContext(), getString(R.string.network_error_txt), Toast.LENGTH_SHORT).show();
			}
		};
		return handler;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_group_setting);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			groupData = (GroupInfo)bundle.getSerializable("groupData");
			init();
		}else{
			finish();
			Toast.makeText(getApplicationContext(), "数据异常", Toast.LENGTH_SHORT).show();
		}
	}
	
	private void init(){
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		btn_quit_group = (Button)findViewById(R.id.btn_quit_group);
		if(groupData.getUid().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			btn_quit_group.setText("解散群组");
		}else{
			btn_quit_group.setText("退出群组");
		}
		clear_msg = (RelativeLayout)findViewById(R.id.clear_msg);
		invite_member = (RelativeLayout)findViewById(R.id.invite_member);
		tousu = (RelativeLayout)findViewById(R.id.tousu);
		checkbox_msg_recevied = (CheckBox)findViewById(R.id.checkbox_msg_recevied);
		checkbox_msg_tips = (CheckBox)findViewById(R.id.checkbox_msg_tips);
		
		checkbox_msg_recevied.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if(arg1){
					try {
						EMGroupManager.getInstance().blockGroupMessage(groupData.getGroupid());
					} catch (EaseMobException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					try {
						EMGroupManager.getInstance().unblockGroupMessage(groupData.getGroupid());
					} catch (EaseMobException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
		checkbox_msg_tips.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				List<String> groups = new ArrayList<String>();
				groups.add(groupData.getGroupid());
				if(arg1){
					EMChatManager.getInstance().getChatOptions().setGroupsOfNotificationDisabled(groups);
				}else{
					EMChatManager.getInstance().getChatOptions().setGroupsOfNotificationDisabled(new ArrayList<String>());
				}
			}
		});
		
		clear_msg.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(GroupSettingActivity.this)
				.setTitle("提示")
				.setMessage("确定清空所有聊天记录？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						EMChatManager.getInstance().clearConversation(groupData.getGroupid());
						Toast.makeText(getApplicationContext(), "清空成功", Toast.LENGTH_SHORT).show();
					}
				}).show();
			}
		});
		invite_member.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),GroupInviteActivity.class);
				//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				Bundle bundle = new Bundle();
				bundle.putSerializable("groupData", groupData);
				intent.putExtras(bundle);
				startActivity(intent);
				
			}
		});
		tousu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(GroupSettingActivity.this)
				.setTitle("提示")
				.setMessage("确定清空所有聊天记录？")
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
					}
				})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						EMChatManager.getInstance().clearConversation(groupData.getGroupid());
						Toast.makeText(getApplicationContext(), "举报成功", Toast.LENGTH_SHORT).show();
					}
				}).show();
			}
		});
		btn_quit_group.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RequestParams params = new RequestParams();
				params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				progressDialog = ProgressDialog.show(GroupSettingActivity.this, "提示", "正在操作...", true, false);
				if(groupData.getUid().equals(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
					params.put("groupid", groupData.getGroupid());
					HttpClient.getInstance().get(Constants.API_ADD_DEL_CHART_URL, params, getHandler());
				}else{
					params.put("group_id", groupData.getGroupid());
					HttpClient.getInstance().get(Constants.API_ADD_REMOVECHART_USER_URL, params, getHandler());
				}
				
			}
		});
	}	
	
}
